package com.my.aslife.controller;


import com.my.aslife.module.common.Condition;
import com.my.aslife.module.common.ErrorCode;
import com.my.aslife.module.common.PageRequestVo;
import com.my.aslife.module.common.ResponseResult;
import com.my.aslife.module.entity.Demand;
import com.my.aslife.module.entity.dtos.DemandAcceptDto;
import com.my.aslife.module.entity.dtos.DemandAddDto;
import com.my.aslife.module.entity.dtos.DemandQueryDto;
import com.my.aslife.module.entity.dtos.DemandUpdateDto;
import com.my.aslife.service.DemandService;
import com.my.aslife.utils.MyUtils;
import lombok.extern.slf4j.Slf4j;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yuan.ma
 * @since 2022-12-12
 */
@RestController
@RequestMapping("/api/my/demand")
@CrossOrigin
@Slf4j
public class DemandController {

    @Autowired
    private DemandService demandService;

    @GetMapping("/getAll/list")
    public ResponseResult getAllDemandInfo(@RequestParam Integer current, @RequestParam Integer pageSize) {

        PageRequestVo pageRequestVo = new PageRequestVo(current, pageSize);
        return demandService.getAllDemandInfo(pageRequestVo);
    }


    @PostMapping("/list/{uid}")
    public ResponseResult getDemandInfoForList(@PathVariable Integer uid) {
        log.info("{}", uid);
        return demandService.getDemandInfoForList(uid);
    }

    @PostMapping("/insert")
    public ResponseResult insertDemandInfo(@RequestBody DemandAddDto demandAddDto){
        if (MyUtils.allFieldIsNULL(demandAddDto)) {
            return ResponseResult.failed(ErrorCode.APP_PARAMS_NOT_NULL);
        }
        return demandService.insertDemandInfo(demandAddDto);
    }

    @PostMapping("/query")
    public ResponseResult queryDemandInfo(@RequestBody DemandQueryDto demandQueryDto){
        if (MyUtils.allFieldIsNULL(demandQueryDto)) {
            return ResponseResult.failed(ErrorCode.APP_PARAMS_NOT_NULL);
        }
        return demandService.queryDemandInfo(demandQueryDto);
    }

    @PostMapping("/update/address")
    public ResponseResult updateAddressByDemandKey(@RequestBody DemandUpdateDto demandUpdateDto){
        if (demandUpdateDto.getAddressKey() == null || MyUtils.allFieldIsNULL(demandUpdateDto.getAddress())) {
            return ResponseResult.failed(ErrorCode.C_PARAMS_NOT_NULL);
        }
        return demandService.updateAddress(demandUpdateDto);
    }

    @PostMapping("/delete/{addressKey}")
    public ResponseResult deleteDemandByAddressKey(@PathVariable String addressKey){
        return demandService.deleteDemandByAddressKey(addressKey);
    }

    @PostMapping("/ok/list/{uid}")
    public ResponseResult getPassDemandList(@PathVariable Long uid) {
        return demandService.getPassDemandList(uid);
    }

    @PostMapping("/accept")
    public ResponseResult acceptDemand(@RequestBody DemandAcceptDto demandAcceptDto){
        return demandService.acceptDemand(demandAcceptDto);
    }

    @PostMapping("/run/ok")
    public ResponseResult runOk(@RequestBody Demand demand) {
        return demandService.runOk(demand);
    }

    @PostMapping("/admin/ok/{orderId}")
    public ResponseResult adminOk(@PathVariable Long orderId) {
        return demandService.adminOk(orderId);
    }

}
